package action;

import controller.Action;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Curso;
import model.Disciplina;
import padraoMemento.MementoDisciplina;

public class PesquisarDisciplinaAction extends Action{

    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException {
       
        try {
           int filtro = Integer.parseInt(request.getParameter("filtro"));
          String  usuario = request.getParameter("usuario");
           request.setAttribute("cursos", Curso.obterCursos());
           
           if( filtro==1){
               int curso = Integer.parseInt(request.getParameter("curso"));
               String nomeDisciplina = request.getParameter("disciplina");
               if((curso == 0 ) && (nomeDisciplina==null || nomeDisciplina.equals("")) ){
                request.setAttribute("disciplinas", Disciplina.obterDisciplinas());
               }else{
                    if(curso!=0 && !nomeDisciplina.equals("")){
                        request.setAttribute("disciplinas", Disciplina.obterDisciplinasPorCursoENome(curso,nomeDisciplina));
                        request.setAttribute("cursoFiltro", curso);
                        request.setAttribute("nomeFiltro", nomeDisciplina);
                    }else if(curso!=0){
                        request.setAttribute("disciplinas", Disciplina.obterDisciplinasPorCurso(curso));
                        request.setAttribute("cursoFiltro", curso);
                    }else if(!nomeDisciplina.equals("")){
                        request.setAttribute("disciplinas", Disciplina.obterDisciplinasPorNome(nomeDisciplina));
                        request.setAttribute("nomeFiltro", nomeDisciplina);
                    }
               }
           }else{
                
                   request.setAttribute("disciplinas", Disciplina.obterDisciplinas());
                  
             
           }
               
           
           
           RequestDispatcher view = request.getRequestDispatcher("/pesquisarDisciplina.jsp");
           view.forward(request,response);
           
        } catch (ServletException e) {
            throw e;
        } catch (IOException e) {
            throw new ServletException(e);
        } catch (SQLException e) {
            throw new ServletException(e);
        } catch (ClassNotFoundException e) {
            throw new ServletException(e);
        }
    }

    @Override
    public void prepararOperacao(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void confirmarOperacao(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}
